/*
 * @(#)PasswordUtil.java 1.0 2007-8-15
 * Copyright (c) 2007 BOCSoft Information Industrial (Shenzhen) Co., Ltd.
 * All rights reserved.
 */

package com.fescoservice.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.binary.Base64;

public class PasswordUtil {
	

	public static void main(String args[]){
		
		Date now = new Date();
		SimpleDateFormat dateformat=new SimpleDateFormat("MM/dd/yyyy");
		String nowDate=dateformat.format(new Date());

		System.out.println("nowDate:"+nowDate.toString());
//		System.out.println(PasswordUtil.dateToString(nowDate));
		
		
		System.out.println(PasswordUtil.encryptPassword("1", "1"));
		System.out.println(PasswordUtil.encryptPassword("2", "2"));
		System.out.println(PasswordUtil.encryptPassword("3", "3"));
		System.out.println(PasswordUtil.encryptPassword("4", "4"));
		System.out.println(PasswordUtil.encryptPassword("5", "5"));
	}

	/** 密码最小长度 */
	private static final int passMinLen = 8;
	/** 密码最大长度 */
	private static final int passMaxLen = 20;
	/**　随机数发生器　*/
	private static SecureRandom random = new SecureRandom();

	/** 原始口令二次散列
	 * @param loginName 登录名
	 * @param password 原始口令
	 * @return
	 */
	public static String encryptPassword(String loginName, String password) {
		return reEncryptPassword(loginName, encrypt(password));
	}

	/** SHA-1散列
	 * @param value
	 * @return
	 */
	public static byte[] digestSHA1(String value) {
		try {
			MessageDigest messagedigest = MessageDigest.getInstance("SHA-1");
			byte[] strByte;
			try {
				strByte = value.getBytes("GBK");
			} catch (UnsupportedEncodingException ex) {
				strByte = value.getBytes();
			}
			return messagedigest.digest(strByte);
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}


	/** 中间密码散列
	 * @param loginName 登录名
	 * @param hash 中间密码
	 * @return
	 */
	public static String reEncryptPassword(String loginName, String hash) {
		return new String(Base64.encodeBase64(digestSHA1(loginName + ' ' + hash), false));
	}

	/** 散列
	 * @param string
	 * @return
	 */
	public static String encrypt(String string) {
		return new String(Base64.encodeBase64(digestSHA1(string), false));
	}

	/** 根据格式判断是否可能是被加密过的口令
	 * @param string
	 * @return
	 */
	public static boolean isEncrypted(String string) {
		return string != null && string.length() > passMaxLen;
	}
	/** 检查密码强度
	 * @param password
	 * @return true 强度满足要求
	 */
	public static boolean isStrongPassword(String password) {
		return isStrongPassword(password.toCharArray());
	}

	/** 检查密码强度(字母+数字)
	 * @param password
	 * @return true 强度满足要求
	 */
	protected static boolean isStrongPassword(char[] password) {
		if (password.length < passMinLen || password.length > passMaxLen)
			return false;

		int flag = 0;
		for (int i = 0; i < password.length; i++) {
			if (password[i] < '!' || password[i] > '~')
				return false;
			if (password[i] >= '0' && password[i] <= '9')
				flag |= 1;
			else if (password[i] >= 'A' && password[i] <= 'Z'
					|| password[i] >= 'a' && password[i] <= 'z')
				flag |= 2;
		}
		return 3 == flag;
	}

	/** 产生随机密码 ，如果长度不满足配置要求抛出IllegalArgumentException异常 为了便于识别，不包括标点符号
	 * @param length 指定长度
	 * @return
	 * @throws IllegalArgumentException
	 */
	public static String randomPassword(int length) {
		if (length < passMinLen || length > passMaxLen)
			throw new IllegalArgumentException("Password length illegal.");
		char[] pass = new char[length];
		int c;
		do {
			for (int i = 0; i < length; i++) {
				c = (random.nextInt(62) + '0');
				if (c > '9')
					c += 'A' - '9' - 1;
				if (c > 'Z')
					c += 'a' - 'Z' - 1;
				if (c == '0' || c == '1' || c == 'O' || c == 'l')
					continue;
				pass[i] = (char) c;
			}
		} while (!isStrongPassword(pass));
		return new String(pass);
	}

	/** 产生随机密码 长度不限制，按照配置
	 * @return
	 */
	public static String randomPassword() {
		return randomPassword(random.nextInt(passMaxLen - passMinLen + 1)
				+ passMinLen);
	}
}
